
from lib.api.common import *
from config.config import host,host1,host2,host3,host4,host5,localhost
import requests,copy
#测试
class Bolck:
    #添加
    def add(self,payload,timestemp,host):
        url=f'{host}/block/create'
        payload1=copy.copy(payload)
        #headers=getHeaders(payload1,timestemp,host)
        headers = getHeadersNow(payload,timestemp)
        res=requests.post(url,headers=headers,json=payload1)
        return res.json()
    #获取最后一个块信息
    def getLast(self,timestemp,host):
        url=f'{host}/block/last'
        #headers=getHeadersNoPara(timestemp,host)
        headers = getHeadersNoParaNow(timestemp)
        res=requests.get(url,headers=headers)
        return res.json()
    ...#本地查询块...
    def getDataByLocal(self,timestemp,host,payload):
        payload1=copy.copy(payload)
        url=f'{host}/block/local/{payload1}'
        #headers = getHeadersNoPara(timestemp, host)
        headers = getHeadersNoParaNow(timestemp)
        res=requests.get(url,headers=headers)
        return res.json()
    #链上查询块
    def getDataByOnline(self,timetemp,host,payload):
        payload1=copy.copy(payload)
        url=f'{host}/block/chain/{payload1}'
        #headers = getHeadersNoPara(timestemp, host)
        headers = getHeadersNoParaNow(timetemp)
        res=requests.get(url,headers=headers)
        return res.json()

        # 验证在链上查询
    #验证查询
    def virifyTheSelect(self, timetemp, para_host, bolckhash):
        res = bolck.getDataByLocal(timetemp, para_host, bolckhash)
        if 'data' in res.keys() and res['data']!=None  and  len(res['data']) > 0:
            showContent = {}
            showContent["busiType"] = res["data"][0]["busiType"]
            showContent["content"] = res["data"][0]["content"]
            print(f'在{para_host}本地库查询数据:\n {showContent}')
        else:
            print(f'在{para_host}本地库查询数据失败:\n {res}')
        # 链上区块查询
        res = bolck.getDataByOnline(timetemp, para_host, bolckhash)
        if 'data' in res.keys() and res['data']!=None  and len(res['data']) > 0:
            print(f'在{para_host}链上查询数据:\n {jsonformat(res["data"]["blockBody"]["transactions"][0]["json"])}\n')
        else:
            print(f'在{para_host}链上查询数据失败:\n {res}\n')
    #验证添加
    def verifyTheAdd(self,payload1,timestemp,def_host):
        payload=copy.copy(payload1)
        res = bolck.add(payload, timestemp, def_host)
        if 'code' not in res.keys() or  res['code'] != 200:
            print(f'{def_host}添加失败,返回结果：{jsonformat(res)}')
        else:
            bolckhash = res["data"]["hash"]
            print(f'在{def_host}添加完成:   内容为{content}   hash值为：{bolckhash}\n')
            # 在各个联盟成员中的本地与链上查询区块
            for i in range(1, 6):
                _host = host[f"{i}"]
                self.virifyTheSelect(timestemp,_host,bolckhash)

if __name__ == '__main__':
    timestemp = getTimesTemp()
    print("自动生成的毫秒数"+timestemp)
    bolck=Bolck() #实例化对象
    '''添加'''
    # content=f'{getNow()}转账8735元'
    # payload={"content":content,"busiType":"ZX"}
    # bolck.verifyTheAdd(payload,timestemp,host1)
    #payload = {"content": '转正金额', "busiType": "123"}
    # payload={'content': '转账金额500元', 'busiType': 'ZX'}
    # res=bolck.add(payload, timestemp, host1)
    # print(jsonformat(res['data']['hash']))
    '''查询'''
    bolckhash = ['d4d355ba16fadf93cc599769fc7d64b02905d2c5720c63d08d2caa502574f82d','c48f29b4d6b95637f020e3a8ebf8f5594538aaaa4a3bba16be55fbcd99d0bedb']
    print(f"bolckhash的值为：{bolckhash[1]}")
    for i in range(1,6):
        bolck.virifyTheSelect(timestemp,host[f"{i}"],bolckhash[0])

    # res=bolck.getDataByLocal(timestemp,host5,bolckhash[0])
    # print(res)
    #print(jsonformat(res['data'][0]['belongBlock']))

    #res=bolck.getDataByOnline(timestemp,host1,bolckhash[0])
    #print(res)

    '''获取最后一个块信息'''
    # print("获取最后一个模块")
    # res=bolck.getLast(timestemp,host1)
    # last=res['data']['blockBody']['transactions'][0]
    # print(f'获取后的最后块信息为：\n {jsonformat(last)}\n')
    # '''在本地库查询'''
    # for i in  range(0,4):
    #     print(i)